//
//  BMHAlertView.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/3/22.
//  Copyright © 2017年 Baomihua. All rights reserved.
//

#import "BMHAlertView.h"
#import <Masonry.h>

@interface BMHAlertView ()
@property (nonatomic, assign) BMHAlertViewType type;
@property(nonatomic,strong) UIImageView *maskImageView;
@end

@implementation BMHAlertView

- (instancetype)initWithOverType:(BMHAlertViewType)type touchShadeClose:(BOOL)touchShadeClose {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _type = type;
        self.backgroundColor = [UIColor clearColor];
        _maskImageView = [[UIImageView alloc]init];
        _maskImageView.alpha = 0.5;
        _maskImageView.backgroundColor = [UIColor blackColor];
//        _maskImageView.image = [UIImage imageCutScreen];
        [self addSubview:_maskImageView];
        [_maskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        _contentView = [[UIView alloc] init];
//        _contentView.backgroundColor = [UIColor purpleColor];
        [self addSubview:_contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
            //make.center.equalTo(self);
           //make.size.mas_equalTo(CGSizeMake(200, 80));
        }];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panTouch:)];
        [self addGestureRecognizer:tapGesture];
        
        //设置高度
        [self open];

    }
    return self;
}

- (void)panTouch:(UITapGestureRecognizer *)tapGesture {
//    if (self.contentView.subviews.count == 0) return;
    UIView *view = [self.contentView.subviews lastObject];
    CGPoint touchPoint = [tapGesture locationInView:self];
    if (!CGRectContainsPoint(view.frame, touchPoint)) {
        [self close];
    }
}

#pragma mark - 打开动画
- (void)open
{
    switch (_type) {
        case BMHAlertViewTypePopover:{
            self.contentView.layer.transform = CATransform3DMakeScale(0.95, 0.95, 1);
            [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:12 options:UIViewAnimationOptionCurveLinear animations:^{
                self.contentView.layer.transform = CATransform3DMakeScale(1, 1, 1);
            } completion:^(BOOL finished) {
            }];
        }
            break;
        default:{
            self.alpha = 0;
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 1;
            }];
        }
            break;
    }
}

#pragma mark - 关闭动画
- (void)close
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewWillClose:)]) {
        [self.delegate alertViewWillClose:self];
    }
    switch (_type) {
        case BMHAlertViewTypePopover:{
            [UIView animateWithDuration:0.4 animations:^{
                self.contentView.transform =  CGAffineTransformMakeScale(0.95, 0.95);
                self.contentView.alpha = 0;
                self.maskImageView.alpha = 0;
            } completion:^(BOOL finished) {
                [self clear];
            }];
        }
            break;
        default:{
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 0;
            } completion:^(BOOL finished) {
                [self clear];
            }];
        }
            break;
    }
}

-(void)clear{
    [self removeFromSuperview];
    if (self.delegate && [self.delegate respondsToSelector:@selector(alertViewDidClose:)]) {
        [self.delegate alertViewDidClose:self];
    }
}

- (void)dealloc{
    NSLog(@"%@", [self class]);
}

@end
